<!-- 
    @file: 后台顶部-移动端
    @auth: lizibin
 -->
<template>
    <!-- 上侧菜单颜色 可以做成可配置 -->
    <div style="height: 70px;box-shadow: rgb(0 0 0 / 10%) 0px 0px 10px;background: #0e111b;color: #fff;">
        <div class="linkBox">
            <el-popover :width="300"
                popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;">
                <template #reference>
                    <el-link :underline="false" @click="toPersonSetup">
                        <!-- 用户没设置头像的情况下使用系统默认的 -->
                        <img v-if="state.userInfo.userIcon === null || state.userInfo.userIcon === ''"
                            src="../../../assets/default_avatar.png" style="width: 40px;border-radius: 50px;">
                        <img v-if="state.userInfo.userIcon !== null && state.userInfo.userIcon !== ''"
                            :src="state.url + state.userInfo.userIcon" style="width: 40px;border-radius: 50px;">
                        <span>{{ state.userInfo.userName }}</span>
                    </el-link>
                </template>
                <template #default>
                    <div class="demo-rich-conent" style="display: flex; gap: 16px; flex-direction: column">
                        <div class="demo-rich-conent-custom">
                            <!-- 用户没设置头像的情况下使用系统默认的 -->
                            <img v-if="state.userInfo.userIcon === null || state.userInfo.userIcon === ''"
                                src="../../../assets/default_avatar.png">
                            <img v-if="state.userInfo.userIcon !== null && state.userInfo.userIcon !== ''"
                                :src="state.url + state.userInfo.userIcon">
                            <p class="demo-rich-content__name">
                                用户名：{{ state.userInfo.userName }}
                            </p>
                            <p class="demo-rich-content__mention">
                                Email：{{ state.userInfo.userEmail }}
                            </p>
                            <p class="demo-rich-content__mention">
                                个性签名：{{ state.userInfo.userSignature }}
                            </p>
                        </div>
                        <p class="demo-rich-content__desc" style="margin: 0">
                        <div
                            style="float:left;width: 75px;padding:10px;border-right: 1px solid rgb(205, 205, 205);text-align: center;">
                            <p>文章</p>
                            <p style="font-size: 25px;font-weight:600;">
                                {{ state.articleCount }}
                                <span style="font-size: 10px;font-weight:100;margin-left:5px;">篇</span>
                            </p>
                        </div>
                        <div
                            style="float:left;width: 80px;padding:10px;border-right: 1px solid rgb(205, 205, 205);text-align: center;">
                            <p>注册</p>
                            <p style="font-size: 25px;font-weight:600;">
                                {{ handlerDateDurationCurrent(state.userInfo.userRegdate) }}
                                <span style="font-size: 10px;font-weight:100;margin-left:5px;">天</span>
                            </p>
                        </div>
                        <div style="float:left;width: 75px;padding:10px;text-align: center;">
                            <p>类型</p>
                            <p style="font-size: 15px;font-weight:600;line-height: 40px;">
                                <span v-if="state.userInfo.userType == 0">管理员</span>
                                <span v-if="state.userInfo.userType == 1">普通用户</span>
                            </p>
                        </div>
                        </p>
                    </div>
                </template>
            </el-popover>

            <el-link :underline="false" @click="returnHome">
                <el-icon>
                    <Promotion />
                </el-icon>
                <span>返回首页</span>
            </el-link>

            <el-popconfirm confirm-button-text="确认" cancel-button-text="取消" :icon="SwitchButton" icon-color="#30bcd7"
                title="确认退出登录嘛?" @confirm="exit">
                <template #reference>
                    <el-link :underline="false">
                        <el-icon>
                            <SwitchButton />
                        </el-icon>
                        <span>退出</span>
                    </el-link>
                </template>
            </el-popconfirm>
        </div>
    </div>
</template>

<script lang="ts" setup>
import {
    reactive,
    onMounted
} from 'vue'
import {
    useRoute,
    useRouter
} from "vue-router"
import {
    SwitchButton
} from '@element-plus/icons-vue'
import { useStore } from '@/store/index'
import { storeToRefs } from 'pinia'
import Cookies from 'js-cookie'
import { GetArticleCountByUserId } from '@/api/backstage/index'

const Store = useStore()
const { LoginInfo } = storeToRefs(Store)
const { code: LoginCode, data: LoginData } = LoginInfo.value;
const router = useRouter();
const route = useRoute();

const state = reactive({
    // 当前登录的用户信息
    userInfo: {
        userEmail: '',
        userIcon: '',
        userPass: '',
        userRegdate: '',
        userSignature: '',
        userId: -1,
        userType: -1,
        userName: '',
    },
    // 当前登录用户编写文章总数量
    articleCount: -1,
    // 服务器路径
    url: import.meta.env.VITE_HTTP,
})
onMounted(() => {
    // 获取用户信息
    const userInfo = LoginData.data[0];
    state.userInfo = userInfo;

    // 查询当前登录用户编写文章总数量
    const params = {
        userId: Number(Cookies.get('LoginId'))
    }
    GetArticleCountByUserId(params).then(res => {
        state.articleCount = res.data < 10 ? 0 + res.data : res.data
    })
});


const handlerDateDurationCurrent = (time: any) => {
    let d1 = new Date(time)
    let d2 = new Date()
    let cha = Math.abs(d2.getTime() - d1.getTime())
    let days = cha / (24 * 60 * 60 * 1000)
    let hours = cha % (24 * 60 * 60 * 1000) / (60 * 60 * 1000)
    let mins = cha % (60 * 60 * 1000) / (60 * 1000)
    
    return Math.ceil(days) 
}

// 点击头像前往用户设置
const toPersonSetup = () => {
    router.push({
        path: '/personSetup',
    })
}

// 返回首页
const returnHome = () => {
    router.push({
        path: '/',
    })
}
// 退出登录
const exit = () => {
    Store.loginOut()
    // 返回首页
    router.push({
        path: '/',
    })
}
</script>

<style scoped>
@media screen and (max-width: 768px) {
    .linkBox {
        margin: 0 auto !important;
        float: none !important;
    }
}

.linkBox {
    height: 100%;
    display: flex;
    align-items: center;
    text-align: center;
    float: right;
}

.linkBox .el-link {
    margin-right: 25px;
    color: #8c8c8c;
}

.linkBox .el-link:hover {
    color: #30bcd7;
}

.linkBox .el-link span {
    margin-left: 8px;
}

/* 鼠标滑过头像的样式 */
.demo-rich-conent-custom img {
    width: 55px;
    margin: 0px 15px 0px 0px;
    border-radius: 50px;
    float: left;
}

.demo-rich-conent-custom p {
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>